package com.tigase.controller;/**
 * Created by tianfeng on 2017/7/20.
 */

import com.tigase.bean.TigaseUser;
import com.tigase.service.FriendService;
import com.tigase.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tianfeng
 * @create 2017-07-20 14:11
 **/
@RestController
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService FriendService;
    @Autowired
    private UserService UserService;

    /**
     * 获取好友列表
     * @param
     * @return
     */
    @RequestMapping(value = "/getFriendList/{userName}" )
    public Map<String,Object> getFriendList(@PathVariable(value = "userName")String userName, HttpServletResponse HttpServletResponse){
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map=new HashMap<>();
        List<Map<String, String>> result=FriendService.getFriendList(userName);
        if(result.size()>0){
                map.put("result",result);
        }else{
            map.put("result","null");
        }
        map.put("msg","好友列表获取成功");
        map.put("flag",true);
        return map;
    }

    /**
     * 添加好友,等待对方回应
     * @param
     * @param
     * @return
     */
    @RequestMapping(value="/addFriend/{userName}/{friendName}/{statuss}")
    public Map<String,Object> addFriend(@PathVariable(value = "userName")String userName,@PathVariable(value = "friendName")String friendName,@PathVariable(value = "statuss")String statuss, HttpServletResponse HttpServletResponse){
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map=new HashMap<>();
        TigaseUser user=UserService.registerUser(userName);
        TigaseUser userFriend=UserService.registerUser(friendName);
        if(null==userFriend|| StringUtils.isEmpty(userFriend.getUserName())){
            map.put("msg","好友不存在");
            map.put("flag",false);
            return map;
        }else {
            List<Map<String, Object>> res= FriendService.getFriend(user.getUserId(),userFriend.getUserId());

           if(res.size()>0){
              int status= (int) res.get(0).get("friend_status");
              if(status==0){
                  map.put("msg","您已经添加过该好友");
                  map.put("flag",true);
              }else{
                  map.put("msg","对方已是您的好友");
                  map.put("flag",false);
              }
           }else{
               FriendService.insertFriend(user.getUserId(),userFriend.getUserId(),statuss);
               System.out.println("添加好友" );
               map.put("msg","好友添加成功,等待好友同意");
               map.put("flag",true);
           }
        }
        return map;
    }

    /**
     * 对方回应，好友添加成功，更新好友状态
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/updateFriend/{userName}/{friendName}")
    @ResponseBody
    public Map<String,Object> updateFriend(@PathVariable(value = "userName")String userName,@PathVariable(value = "friendName")String friendName,HttpServletResponse HttpServletResponse){
        HttpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map=new HashMap<>();
        TigaseUser user=UserService.registerUser(userName);
        TigaseUser userFriend=UserService.registerUser(friendName);
        FriendService.updateFriend(user.getUserId(),userFriend.getUserId());
        map.put("msg","好友添加成功");
        map.put("flag",true);
        return map;
    }
}
